package Test01;

import java.util.ArrayList;
import java.util.Scanner;

public class followSMS {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        while (true) {
            showMenu();
            int option = sc.nextInt();
            switch (option) {
                case 1:
                    add(list,sc);
//                    System.out.println("添加成功");
                    break;
                case 2:
                    deleteById(list,sc);
//                    System.out.println("删除成功");
                    break;
                case 3:
                    updateById(list,sc);
//                    System.out.println("修改成功");
                    break;
                case 4:
                    selectAll(list);
//                    System.out.println("查看成功");
                    break;
                case 5:
                    System.out.println("谢谢使用,欢迎下次光临");
                    System.exit(0);//关闭JVM
                default:
                    System.out.println("目前系统尚不支持您输入的操作,请重新选择");
                    break;
            }
        }
    }

    private static void updateById(ArrayList<Student> list, Scanner sc) {
        if ( list == null || list.isEmpty() || sc == null ) {
            return;
        }
        System.out.println("请输入要修改信息的学生的id");
        String id = sc.next();
        Student stu = selectById(list, id);
        if (stu == null) {
            System.out.println("您输入的要修改的学号不存在,无法完成修改操作,请选择其它操作");
            return;
        }
        System.out.println("请输入新的姓名:");
        String name = sc.next();
        System.out.println("请输入新的年龄:");
        String age = sc.next();
        System.out.println("请输入新的地址:");
        String address = sc.next();

        stu.setName(name);
        stu.setAge(age);
        stu.setAddress(address);

        System.out.println("修改成功");

    }

    private static void deleteById(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入要删除的学生的id");
        String id = sc.next();
        //根据id查询学生Student对象
        Student stu = selectById(list, id);
        if (stu == null) {
            System.out.println("您输入的要删除的学号不存在,无法完成删除操作,请选择其它操作");
            return;
        }
        list.remove(stu);
        System.out.println("删除成功");

    }

    private static void selectAll(ArrayList<Student> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        System.out.println("学号\t姓名\t年龄\t地址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getSid()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }

    private static void add(ArrayList<Student> list, Scanner sc) {
        System.out.println("请输入学号:");
        String id = sc.next();
        //stu是null,说明id没有被使用,可以使用
        while (true) {
            //根据id查询对应的Student对象
            Student stu = selectById(list, id);
            //判断stu不为空,说明id值已经被占用
            if (stu != null) {
                //循环再次输入id值
                System.out.println("您输入的id值已经被使用,请重新录入");
                id = sc.next();
            } else {
                //stu等于null,说明id值没有被占用,可以用
                break;
            }
        }
        System.out.println("请输入新的姓名:");
        String name = sc.next();
        System.out.println("请输入新的年龄:");
        String age = sc.next();
        System.out.println("请输入新的地址:");
        String address = sc.next();
        //创建Student对象,封装以上获取到的学生的信息
        Student stu = new Student(id, name, age, address);
        //把Student对象存储ArrayList集合对象list中
        list.add(stu);
        System.out.println("添加成功");
    }

    private static Student selectById(ArrayList<Student> list, String id) {
        if ( id == null || list == null || list.isEmpty()) {
            return null;
        }
        //使用for遍历ArrayList集合对象
        for (int i = 0; i < list.size(); i++) {
            //获取当前Student对象
            Student stu = list.get(i);
            //判断当前Student对象的id值和方法参数id值相同,说明找到了,直接返回当前Student对象
            if (stu.getSid().equals(id)) {
                return stu;
            }
        }
        return null;
    }

    private static void showMenu() {
        System.out.println("--------欢迎来到学生管理系统--------");
        System.out.println("1.添加学生");
        System.out.println("2.删除学生");
        System.out.println("3.修改学生");
        System.out.println("4.查看所有学生");
        System.out.println("5.退出");
        System.out.println("请输入你的选择");
    }
}
